home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-23 | 11.3 KB | 387 lines |
- /* -----------------------------------------------------------------------
- A Java Adventure Game framework.
- -----------------------------------------------------------------------
- Sample Java program for PC Plus magazine.
- Author: Huw Collingbourne
-
- This program is designed with the intention of keeping
- the visual design separate, as much as possible, from the
- detailed implementation of the game itself.
-
- Although tbis is the 'main' file, it contains very little of the
- real code of the game. It is mostly confined to the visual
- interface. The code is mainly auto-generated by Visual Cafe 2.0
-
- My 'hand-written' code can be found at the bottom of this file.
-
- ----------------------------------------------------------------------- */
-
- import java.awt.*;
- import java.io.*;
- import java.util.*;
-
- public class MainFrame extends Frame
- implements AdventureConstants
- {
-
-
- public MainFrame()
- {
- // This code is automatically generated by Visual Cafe when you add
- // components to the visual environment. It instantiates and initializes
- // the components. To modify the code, only use code syntax that matches
- // what Visual Cafe can generate, or Visual Cafe may be unable to back
- // parse your Java file into its visual environment.
-
- //{{INIT_CONTROLS
- setLayout(null);
- setVisible(false);
- setSize(insets().left + insets().right + 405,insets().top + insets().bottom + 305);
- openFileDialog1 = new java.awt.FileDialog(this);
- openFileDialog1.setMode(FileDialog.LOAD);
- openFileDialog1.setTitle("Open");
- //$$ openFileDialog1.move(40,277);
- displayBox = new java.awt.TextArea();
- displayBox.setBounds(insets().left + 12,insets().top + 12,384,180);
- displayBox.setFont(new Font("Dialog", Font.PLAIN, 16));
- add(displayBox);
- NBtn = new java.awt.Button();
- NBtn.setActionCommand("button");
- NBtn.setLabel("N");
- NBtn.setBounds(insets().left + 300,insets().top + 204,36,24);
- NBtn.setBackground(new Color(12632256));
- add(NBtn);
- SBtn = new java.awt.Button();
- SBtn.setActionCommand("button");
- SBtn.setLabel("S");
- SBtn.setBounds(insets().left + 300,insets().top + 228,36,24);
- SBtn.setBackground(new Color(12632256));
- add(SBtn);
- WBtn = new java.awt.Button();
- WBtn.setActionCommand("button");
- WBtn.setLabel("W");
- WBtn.setBounds(insets().left + 264,insets().top + 216,36,24);
- WBtn.setBackground(new Color(12632256));
- add(WBtn);
- EBtn = new java.awt.Button();
- EBtn.setActionCommand("button");
- EBtn.setLabel("E");
- EBtn.setBounds(insets().left + 336,insets().top + 216,36,24);
- EBtn.setBackground(new Color(12632256));
- add(EBtn);
- InputTF = new java.awt.TextField();
- InputTF.setBounds(insets().left + 12,insets().top + 192,170,27);
- add(InputTF);
- TakeBtn = new java.awt.Button();
- TakeBtn.setActionCommand("button");
- TakeBtn.setLabel("Take");
- TakeBtn.setBounds(insets().left + 12,insets().top + 228,76,24);
- TakeBtn.setBackground(new Color(12632256));
- add(TakeBtn);
- DropBtn = new java.awt.Button();
- DropBtn.setActionCommand("button");
- DropBtn.setLabel("Drop");
- DropBtn.setBounds(insets().left + 96,insets().top + 228,73,25);
- DropBtn.setBackground(new Color(12632256));
- add(DropBtn);
- LookBtn = new java.awt.Button();
- LookBtn.setActionCommand("button");
- LookBtn.setLabel("Look");
- LookBtn.setBounds(insets().left + 96,insets().top + 264,71,23);
- LookBtn.setBackground(new Color(12632256));
- add(LookBtn);
- setTitle("The Great Java Adventure!");
- //}}
-
- //{{INIT_MENUS
- mainMenuBar = new java.awt.MenuBar();
- menu1 = new java.awt.Menu("File");
- miNew = new java.awt.MenuItem("New");
- menu1.add(miNew);
- miLoad = new java.awt.MenuItem("Load...");
- menu1.add(miLoad);
- miSave = new java.awt.MenuItem("Save");
- menu1.add(miSave);
- miSaveAs = new java.awt.MenuItem("Save As...");
- menu1.add(miSaveAs);
- menu1.addSeparator();
- miExit = new java.awt.MenuItem("Exit");
- menu1.add(miExit);
- mainMenuBar.add(menu1);
- menu3 = new java.awt.Menu("Help");
- mainMenuBar.setHelpMenu(menu3);
- miAbout = new java.awt.MenuItem("About..");
- menu3.add(miAbout);
- mainMenuBar.add(menu3);
- setMenuBar(mainMenuBar);
- //$$ mainMenuBar.move(4,277);
- //}}
-
- //{{REGISTER_LISTENERS
- SymWindow aSymWindow = new SymWindow();
- this.addWindowListener(aSymWindow);
- SymAction lSymAction = new SymAction();
- miLoad.addActionListener(lSymAction);
- miAbout.addActionListener(lSymAction);
- miExit.addActionListener(lSymAction);
- NBtn.addActionListener(lSymAction);
- SBtn.addActionListener(lSymAction);
- WBtn.addActionListener(lSymAction);
- EBtn.addActionListener(lSymAction);
- miSave.addActionListener(lSymAction);
- TakeBtn.addActionListener(lSymAction);
- LookBtn.addActionListener(lSymAction);
- DropBtn.addActionListener(lSymAction);
- SymComponent aSymComponent = new SymComponent();
- LookBtn.addComponentListener(aSymComponent);
- //}}
- updatedisplayBox(""); //!! Show description on starting
-
- }
-
- public MainFrame(String title)
- {
- this();
- setTitle(title);
- }
-
- public synchronized void show()
- {
- move(50, 50);
- super.show();
- }
-
- static public void main(String args[])
- {
- (new MainFrame()).show();
- }
-
- public void addNotify()
- {
- // Record the size of the window prior to calling parents addNotify.
- Dimension d = getSize();
-
- super.addNotify();
-
- if (fComponentsAdjusted)
- return;
-
- // Adjust components according to the insets
- setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
- Component components[] = getComponents();
- for (int i = 0; i < components.length; i++)
- {
- Point p = components[i].getLocation();
- p.translate(insets().left, insets().top);
- components[i].setLocation(p);
- }
- fComponentsAdjusted = true;
- }
-
- // Used for addNotify check.
- boolean fComponentsAdjusted = false;
-
- //{{DECLARE_CONTROLS
- java.awt.FileDialog openFileDialog1;
- java.awt.TextArea displayBox;
- java.awt.Button NBtn;
- java.awt.Button SBtn;
- java.awt.Button WBtn;
- java.awt.Button EBtn;
- java.awt.TextField InputTF;
- java.awt.Button TakeBtn;
- java.awt.Button DropBtn;
- java.awt.Button LookBtn;
- //}}
-
- //{{DECLARE_MENUS
- java.awt.MenuBar mainMenuBar;
- java.awt.Menu menu1;
- java.awt.MenuItem miNew;
- java.awt.MenuItem miLoad;
- java.awt.MenuItem miSave;
- java.awt.MenuItem miSaveAs;
- java.awt.MenuItem miExit;
- java.awt.Menu menu3;
- java.awt.MenuItem miAbout;
- //}}
-
- class SymWindow extends java.awt.event.WindowAdapter
- {
- public void windowClosing(java.awt.event.WindowEvent event)
- {
- Object object = event.getSource();
- if (object == MainFrame.this)
- Frame1_WindowClosing(event);
- }
- }
-
- void Frame1_WindowClosing(java.awt.event.WindowEvent event)
- {
- hide(); // hide the Frame
- dispose(); // free the system resources
- System.exit(0); // close the application
- }
-
- class SymAction implements java.awt.event.ActionListener
- {
- public void actionPerformed(java.awt.event.ActionEvent event)
- {
- Object object = event.getSource();
- if (object == miLoad)
- miLoad_Action(event);
- else if (object == miAbout)
- miAbout_Action(event);
- else if (object == miExit)
- miExit_Action(event);
- else if (object == NBtn)
- NBtn_Action(event);
- else if (object == SBtn)
- SBtn_Action(event);
- else if (object == WBtn)
- WBtn_Action(event);
- else if (object == EBtn)
- EBtn_Action(event);
- else if (object == miSave)
- miSave_Action(event);
- else if (object == TakeBtn)
- TakeBtn_Action(event);
- else if (object == LookBtn)
- LookBtn_Action(event);
- else if (object == DropBtn)
- DropBtn_Action(event);
- }
- }
-
- void miAbout_Action(java.awt.event.ActionEvent event)
- {
- //{{CONNECTION
- // Action from About Create and show as modal
- (new AboutDialog(this, true)).show();
- //}}
- }
-
- void miExit_Action(java.awt.event.ActionEvent event)
- {
- //{{CONNECTION
- // Action from Exit Create and show as modal
- (new QuitDialog(this, true)).show();
- //}}
- }
-
-
- // -------------------------------------------------
- // ----- MY OWN HAND-WRITTEN CODE FROM HERE ON -----
- // -------------------------------------------------
-
- // The Implementor class 'wraps' up all the internal details of
- // the game itself.
- Implementor Imp = new Implementor();
- // The remainder of the code deals with the visual objects
- // on the form.
-
- // --- The displayBox textArea
- void updatedisplayBox(String msg) {
- // !!! argument is now a String - any special message returned
- // by Adventure (e.g. "No Exit!\n")
- //
- // if msg <> "" display the message
- // display description of room anyway
- String s = msg;
- if (s.equals("")) // if no special message show room description
- { // else show message
- Room r = Imp.getAdv().getplayer().getroom();
- Vector thingshere = r.getthings();
- Vector inventory = Imp.getAdv().getplayer().getthings();
- s = "You are in " +
- r.getname() + "\n" +
- r.getdescription() + "\n" +
- "--- Things here ---\n";
- for (Enumeration e = thingshere.elements(); e.hasMoreElements(); )
- s = s + ((Thing)e.nextElement()).getname() + ", ";
- s = s + "\n--- You have ---\n";
- for (Enumeration e = inventory.elements(); e.hasMoreElements(); )
- s = s + ((Thing)e.nextElement()).getname() + ", ";
- }
- displayBox.setText( s + "\n" );
- }
-
- // --- The Direction buttons
- // North
- void NBtn_Action(java.awt.event.ActionEvent event)
- {
- updatedisplayBox(Imp.getAdv().movePlayerTo(NORTH));
- }
-
- // South
- void SBtn_Action(java.awt.event.ActionEvent event)
- {
- updatedisplayBox(Imp.getAdv().movePlayerTo(SOUTH));
- }
-
- // West
- void WBtn_Action(java.awt.event.ActionEvent event)
- {
- updatedisplayBox(Imp.getAdv().movePlayerTo(WEST));
- }
-
- // East
- void EBtn_Action(java.awt.event.ActionEvent event)
- {
- updatedisplayBox(Imp.getAdv().movePlayerTo(EAST));
- }
-
- // Save
- void miSave_Action(java.awt.event.ActionEvent event)
- {
- displayBox.setText("Saving...");
- displayBox.setText(Imp.SaveAdv());
- }
-
- // Load
- void miLoad_Action(java.awt.event.ActionEvent event)
- {
- // openFileDialog1.show(); - may use this later (?)
- displayBox.setText("Loading...");
- displayBox.setText(Imp.LoadAdv());
- }
-
-
-
- void TakeBtn_Action(java.awt.event.ActionEvent event)
- {
- // Take object specified in InputTF
- updatedisplayBox(Imp.getAdv().takeOb(InputTF.getText()));
- }
-
-
- void DropBtn_Action(java.awt.event.ActionEvent event)
- {
- // Drop object specified in InputTF
- updatedisplayBox(Imp.getAdv().dropOb(InputTF.getText()));
- }
-
- void LookBtn_Action(java.awt.event.ActionEvent event)
- {
- // Show room description
- updatedisplayBox("");
- }
-
-
- class SymComponent extends java.awt.event.ComponentAdapter
- {
- public void componentHidden(java.awt.event.ComponentEvent event)
- {
- Object object = event.getSource();
- if (object == LookBtn)
- LookBtn_ComponentHidden(event);
- }
- }
-
- void LookBtn_ComponentHidden(java.awt.event.ComponentEvent event)
- {
- // to do: code goes here.
- }
- }
-
-